package com.jt.util;

import com.jt.exception.ServiceException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author dc
 */
@Service
public class HttpClientService {

	@Autowired
	private CloseableHttpClient httpClient;
	@Autowired
	private RequestConfig requestConfig;

	/**
	 * 编辑一个HttpClientAPI,简化程序远程调用过程
	 * 简化策略:
	 * 	用户使用:传递参数
	 * 	返回结果:远程访问的返回值
	 * 参数说明:
	 * 	arg0:url地址
	 * 	arg1:Map<key,value> 封装方法需求的参数
	 * 	arg2:指定字符集编码
	 * @return 请求数据
	 */
	public String doGet(String url, Map<String,String> params,String charset){
		//1.判断字符集编码是否为空
		if(StringUtils.isEmpty(charset)) {
			charset = "utf-8";
		}
		//2.校验map集合中是否有参数 无参: http://www.jt.com/findItem; 有参:  http://www.jt.com/findItem?id=1&name=tomcat;
		if(params!=null){
			//参数拼接,将Map集合进行遍历,动态获取key和value
			url+="?";
			for (Map.Entry<String,String> entry : params.entrySet()) {
				String key = entry.getKey();
				String value = entry.getValue();
				url+=key+"="+value+"&";
			}
			//取出多余的&符号
			url = url.substring(0,url.length()-1);
		}
		//3.定义用户的请求类型
		HttpGet httpGet=new HttpGet(url);
		//为get添加配置类
		httpGet.setConfig(requestConfig);
		//4.发起http请求
		String result = null;
		try {
			HttpResponse httpResponse = httpClient.execute(httpGet);
			//判断状态码是否正确
			if(httpResponse.getStatusLine().getStatusCode()==200){
				//请求正确
				HttpEntity entity = httpResponse.getEntity();
				//注意字符集编码格式
				result = EntityUtils.toString(entity,charset);

			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServiceException("当前服务器繁忙,请稍后再试");
		}
		return result;
	}

	public String doPost(String url, Map<String,String> params,String charset){
		String result = null;
		//1.定义请求类型
		HttpPost post = new HttpPost(url);
		//定义超时时间
		post.setConfig(requestConfig);
		//2.判断字符集是否为null
		if(StringUtils.isEmpty(charset)){
			charset = "UTF-8";
		}
		//3.判断用户是否传递参数
		if(params !=null){
			//3.2准备List集合信息
			List<NameValuePair> parameters =
					new ArrayList<>();
			//3.3将数据封装到List集合中
			for (Map.Entry<String,String> entry : params.entrySet()) {
				parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
			//3.1模拟表单提交
			try {
				//采用u8编码
				UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,charset);
				//3.4将实体对象封装到请求对象中

				post.setEntity(formEntity);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				throw new ServiceException("服务器繁忙,请稍后再试");
			}
		}
		//4.发送请求
		try {
			CloseableHttpResponse response =
					httpClient.execute(post);

			//4.1判断返回值状态
			if(response.getStatusLine().getStatusCode() == 200) {

				//4.2表示请求成功
				result = EntityUtils.toString(response.getEntity(),charset);
			}else{
				System.out.println("获取状态码信息:"+response.getStatusLine().getStatusCode());
				throw new RuntimeException();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}

	public String doGet(String url){
		return doGet(url,null,null);
	}

	public String doGet(String url,Map<String,String> params){
		return doGet(url,params,null);
	}

	public String doPost(String url){
		return doGet(url,null,null);
	}

	public String doPost(String url,Map<String,String> params){
		return doGet(url,params,null);
	}

}
